package com.luozhili.rabbitmqlearningdemo.base_demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class Send {
    private final static String QUEUE_NAME = "learn_queue_1";

    public static void main(String[] argv) throws Exception {

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.31.226");
            factory.setUsername("zhangsan");
            factory.setPassword("123456");
            factory.setVirtualHost("/shopping");
            try (Connection connection = factory.newConnection() ;//从工厂拿到连接
                 Channel channel = connection.createChannel()//创建管道
                  ) {
                //声明（ 没有则创建） 队列 ，参数：队列名，是否排外（是否私有化），是否自动删除，其它参数
                channel.queueDeclare(QUEUE_NAME, false, false, false, null);
                String message = "Hello World!";
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] Sent '" + message + "'");
            }


    }
}
